package com.mycode.proxy13.resolve;

// 代理查询类：代理主题类
public class ProxySearcher implements Searcher{

    // 维持一个对真实主题的引用
    private Searcher searcher = new RealSearcher();
    private AccessValidator validator;
    private Logger logger;

    @Override
    public String doSearch(String userId, String keyword) {
        // 如果身份验证成功，则执行查询
        if (validate(userId)) {
            String result = searcher.doSearch(userId, keyword);
            this.log(userId);
            return result;
        }
        return null;
    }

    private void log(String userId) {
        logger = new Logger();
        logger.log(userId);
    }

    // 创建访问验证对象并调用
    private boolean validate(String userId) {
        validator = new AccessValidator();
        return validator.validate(userId);
    }
}
